#include <tsin/library/DynamicRandomNumber.hpp>

#include <tsin/util/Logger.hpp>

#include <cstring>
#include <cstdlib>

using namespace tsin;

DynamicRandomNumber::DynamicRandomNumber()
        : start(0.0f), end(0.0f), current(0.0f)
{
}

void DynamicRandomNumber::init(float start, float end)
{
        this->start = start;
        this->end = end;
        random();
}

void DynamicRandomNumber::reinit()
{
        float arg1 = arguments[0]->getFloat();
        float arg2 = arguments[1]->getFloat();

        switch(nrArguments) {
        case 2:
                if (strcmp(desp, "NN") == 0)
                        init(arg1, arg2);
                break;
        }
}

DynamicEvent DynamicRandomNumber::postTick(void *, void *)
{
        random();
        return EVENT_REINIT;
}

char DynamicRandomNumber::getDescriptor()
{
        return 'N';
}

int DynamicRandomNumber::getInt()
{
        return static_cast<int>(current);
}

float DynamicRandomNumber::getFloat()
{
        return current;
}

void DynamicRandomNumber::random()
{
        current = start + ((float)rand() / RAND_MAX) * (end - start);
}

extern "C" LIBRARY_DLLEXPORT DynamicObject *bindRandomNumber()
{
        return new DynamicRandomNumber;
}
